use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use uuid::Uuid;

// 用户相关模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    pub nickname: String,
    pub email: Option<String>,
    pub phone: Option<String>,
    pub avatar: Option<String>,
    pub status: UserStatus,
    pub role_ids: Vec<String>,
    pub dept_id: Option<String>,
    pub dept_name: Option<String>,
    pub create_time: DateTime<Utc>,
    pub update_time: DateTime<Utc>,
    pub last_login_time: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserStatus {
    Active = 0,
    Inactive = 1,
}

impl From<i32> for UserStatus {
    fn from(value: i32) -> Self {
        match value {
            0 => UserStatus::Active,
            _ => UserStatus::Inactive,
        }
    }
}

impl From<UserStatus> for i32 {
    fn from(status: UserStatus) -> Self {
        match status {
            UserStatus::Active => 0,
            UserStatus::Inactive => 1,
        }
    }
}

// 角色相关模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Role {
    pub id: String,
    pub name: String,
    pub code: String,
    pub description: Option<String>,
    pub status: UserStatus,
    pub menu_ids: Vec<String>,
    pub create_time: DateTime<Utc>,
    pub update_time: DateTime<Utc>,
}

// 菜单相关模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Menu {
    pub id: String,
    pub name: String,
    pub path: Option<String>,
    pub component: Option<String>,
    pub icon: Option<String>,
    pub parent_id: Option<String>,
    pub order_num: i32,
    pub menu_type: MenuType,
    pub status: UserStatus,
    pub children: Vec<Menu>,
    pub create_time: DateTime<Utc>,
    pub update_time: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MenuType {
    Directory = 0,
    Menu = 1,
    Button = 2,
}

// SIP 通话相关模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Call {
    pub id: String,
    pub caller: String,
    pub callee: String,
    pub status: CallStatus,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
    pub duration: Option<i64>, // 秒
    pub recording_path: Option<String>,
    pub sip_server: String,
    pub call_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CallStatus {
    Active,
    Ended,
    Failed,
    Ringing,
}

// SIP 注册相关模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SipRegister {
    pub id: String,
    pub username: String,
    pub domain: String,
    pub password: String,
    pub status: RegisterStatus,
    pub register_time: Option<DateTime<Utc>>,
    pub expire_time: Option<DateTime<Utc>>,
    pub last_heartbeat: Option<DateTime<Utc>>,
    pub sip_server: String,
    pub port: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RegisterStatus {
    Registered,
    Unregistered,
    Failed,
    Expired,
}

// 在线用户模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnlineUser {
    pub session_id: String,
    pub user_id: String,
    pub username: String,
    pub nickname: String,
    pub ip_address: String,
    pub login_time: DateTime<Utc>,
    pub last_activity: DateTime<Utc>,
    pub user_agent: String,
}

// 定时任务模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
    pub id: String,
    pub name: String,
    pub job_group: String,
    pub invoke_target: String,
    pub cron_expression: String,
    pub misfire_policy: String,
    pub concurrent: bool,
    pub status: UserStatus,
    pub create_time: DateTime<Utc>,
    pub update_time: DateTime<Utc>,
    pub next_valid_time: Option<DateTime<Utc>>,
}

// 查询参数模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryParams {
    pub page: Option<i32>,
    pub size: Option<i32>,
    pub keyword: Option<String>,
    pub status: Option<String>,
    pub start_time: Option<DateTime<Utc>>,
    pub end_time: Option<DateTime<Utc>>,
}

// 分页响应模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageResult<T> {
    pub list: Vec<T>,
    pub total: i64,
    pub page: i32,
    pub size: i32,
}

// 登录请求模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
    pub captcha: Option<String>,
    pub remember_me: Option<bool>,
}

// 登录响应模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoginResponse {
    pub token: String,
    pub user: User,
    pub permissions: Vec<String>,
}

// 仪表盘数据模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardData {
    pub total_users: i64,
    pub online_users: i64,
    pub total_calls: i64,
    pub active_calls: i64,
    pub system_stats: SystemStats,
    pub recent_calls: Vec<Call>,
    pub recent_users: Vec<User>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStats {
    pub cpu_usage: f64,
    pub memory_usage: f64,
    pub disk_usage: f64,
    pub network_in: f64,
    pub network_out: f64,
}

// 工具函数
impl User {
    pub fn new(username: String, nickname: String) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            username,
            nickname,
            email: None,
            phone: None,
            avatar: None,
            status: UserStatus::Active,
            role_ids: vec![],
            dept_id: None,
            dept_name: None,
            create_time: Utc::now(),
            update_time: Utc::now(),
            last_login_time: None,
        }
    }
}

impl Call {
    pub fn new(caller: String, callee: String, sip_server: String) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            caller,
            callee,
            status: CallStatus::Ringing,
            start_time: Utc::now(),
            end_time: None,
            duration: None,
            recording_path: None,
            sip_server,
            call_id: Uuid::new_v4().to_string(),
        }
    }
}
